﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Web.Security;

public partial class _Default : System.Web.UI.Page
{
    private LMSData.CourseController ccontrol = new LMSData.CourseController();
    private LMSData.UserController ucontrol = new LMSData.UserController();

    protected void Page_Load(object sender, EventArgs e)
    {
        //System.Web.Security.FormsAuthentication.SetAuthCookie("steve", false);
        if (Request.QueryString["Message"] != null)
            lblStatus.Text = Request.QueryString["Message"];
        if(!IsPostBack)
            BindCourses();
    }

    private void BindCourses()
    {
        GridView grdCourses = (GridView)loginview1.FindControl("grdCourses");
        System.Web.Security.MembershipUser musr = null;
        bool located = false;

        try
        {
             musr = Membership.GetUser();
        }
        catch { }

        if (musr != null)
        {
            LMSData.User usr = ucontrol.GetByID((int)musr.ProviderUserKey);

            LMSData.CourseCollection cc = usr.GetCourseCollection();
            LMSData.GroupCollection gc = usr.GetGroupCollection();

            LMSData.GroupCollection coll = new LMSData.GroupController().FetchByQuery(new SubSonic.Query(LMSData.Group.Schema).WHERE(LMSData.Group.Columns.IsPublic, true));
            
            foreach (LMSData.Group grp in coll)
            {
                located = false;
                foreach (LMSData.Group grp2 in gc)
                {
                    if (grp2.GroupID == grp.GroupID)
                        located = true;
                }
                if (!located)
                    gc.Add(grp);
            }

            foreach (LMSData.Group grp in gc)
            {
                foreach(LMSData.Course crs in LMSData.GroupController.GetCourses(grp.GroupID.ToString()))
                {
                    located = false;
                    foreach (LMSData.Course ncrs in cc)
                    {
                        if (ncrs.CourseID == crs.CourseID)
                        {
                            located = true;
                            break;
                        }
                    }
                    if (!located)
                        cc.Add(crs);
                }
            }
            grdCourses.DataSource = cc;
            grdCourses.DataBind();
        }
        UpdatePanel up = (UpdatePanel)loginview1.FindControl("update1");
        if(up != null)
            up.Update();
    }

    protected void LaunchCourse(object sender, CommandEventArgs e)
    {
        Response.Redirect("~/Course/Nav.aspx?NT=W&FN=" + e.CommandArgument.ToString());
    }

    protected void ChangePage(object sender, GridViewPageEventArgs e)
    {
        GridView grdCourses = (GridView)loginview1.FindControl("grdCourses");
        grdCourses.PageIndex = e.NewPageIndex;
        BindCourses();
    }
}
